#!/usr/bin/python
# -*- coding: utf-8 -*-
#Author: Onur ASLAN
#Mail : hata.pindir@gmail.com 

from PyQt4 import QtCore
from PyQt4 import QtGui
from Ui_MainWindow import Ui_MainWindow
from urlparse import urlparse, parse_qs

import getpass
import urllib, urllib2
import os
 
import gdata.youtube, getpass
import gdata.youtube.service


def getImageHQ(self):  
	try:		
		user_name = getpass.getuser()
		url = str(self.lineEdit.text())
		name = str(self.lineEdit_2.text())
	
		down_file = str("/home/"+user_name+"/"+name)
		url_str = urlparse(url)
		url_qstr = parse_qs(url_str.query)
		video_name = url_qstr["v"][0]
		image_url = "http://i1.ytimg.com/vi/"+video_name+"/hqdefault.jpg"
		opener1 = urllib2.build_opener()
		page1 = opener1.open(image_url)
		down_image = page1.read()
		filename = video_name + image_url[-4:]
		#print filename  # test
	
		fout = open("/home/"+user_name+"/"+filename, "wb")
		fout.write(down_image)
		fout.close()
		vid_image = "/home/"+user_name+"/"+filename

		self.video_onizle.setPixmap(QtGui.QPixmap(vid_image))
	
		#fout = open("/home/"+user_name+"/"+".pindir_video_id.txt", "wb")
		#fout.write(video_name)
		#fout.close()
	
	# video get about	
		yt_service = gdata.youtube.service.YouTubeService()
		yt_service.ssl = True
			    	
		entry = yt_service.GetYouTubeVideoEntry(video_id=video_name)

		videoTitle = entry.media.title.text
		videoUploadTime = entry.published.text
		videoDescription = entry.media.description.text
		videoCategory = entry.media.category[0].text
		videoLabel = entry.media.keywords.text
		videoWatchPage = entry.media.player.url
		videoDuration= entry.media.duration.seconds
		videoViewCount = entry.statistics.view_count
	


		self.downVideoDescriptiont.setPlainText(unicode("Özet: \n"+videoDescription))
		self.downVideoTitle.setText(unicode(" Başlık: "+videoTitle))
		self.downVideoViewCount.setText(unicode(" İzlenme Sayısı: "+videoViewCount+" | Uzunluk: "+videoDuration+" sn" ))
		
	except:
		print "Video önizlemesi alınamadı" 

	
def getVideoID(self):
	try:
		user_name = getpass.getuser()
		url = str(self.lineEdit.text())
		name = str(self.lineEdit_2.text())
	
		down_file = str("/home/"+user_name+"/"+name)
		url_str = urlparse(url)
		url_qstr = parse_qs(url_str.query)
		video_name = url_qstr["v"][0]
		image_url = "http://i1.ytimg.com/vi/"+video_name+"/hqdefault.jpg"
		opener1 = urllib2.build_opener()
		page1 = opener1.open(image_url)
		down_image = page1.read()
		filename = video_name + image_url[-4:]
	except:
		print "Video ismi alınamadı" 


	
